import Vuex from 'vuex'
import ledger from '@/net/ledger'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    year: new Date().getFullYear(),
    userAdcd: '',
    cjdm: '',
    adcd: '',
    adcdName: [],
    adcdId: [],
    type: '',
    energyType: '0',
    gridHead: [
      { name: 'adcd', value: '行政区划'},
      { name: '计划改造村数', value: '计划改造村数'},
      { name: '确村数', value: '确村数'},
      { name: '计划改造户数', value: '计划改造户数'},
      { name: '确户数', value: '确户数'}
    ],
    gridData: [],
    energyTypeData: [
      { name: '全部', value: '0'},
      { name: '煤改气', value: '1'},
      { name: '煤改电', value: '2'}
    ]
  },
  mutations: {
    changeYear (state, value) {
      state.year = value
    },
    changeUserAdcd (state, value) {
      state.userAdcd = value
    },
    changeCjdm (state, value) {
      state.cjdm = value
    },
    changeAdcd (state, value) {
      state.adcd = value
    },
    changeAdcdName (state, value) {
      state.adcdName = value
    },
    changeAdcdId (state, value) {
      state.adcdId = value
    },
    changeType (state, value) {
      state.type = value
    },
    changeEnergyType (state, value) {
      state.energyType = value
    },
    changeGridHead (state, value) {
      state.gridHead = value
    },
    changeGridData (state, value) {
      state.gridData = value
    }
  },
  actions: {
    getLedger ({commit, state}, vm) {
      vm.$notice.loading.show()
      ledger.getLedger({
        nd: state.year,
        quyxzqhdm: state.adcd,
        types: state.type,
        quycjdm: state.cjdm
      }).then(res => {
        let arrHead = []
        if (state.energyType === '0') {
          arrHead = [
            { name: 'XZQHMC', value: '行政区划'},
            { name: 'GZCS', value: '计划改造村数'},
            { name: 'GZCS_SJ', value: '确村数'},
            { name: 'GZHS', value: '计划改造户数'},
            { name: 'GZCS_SJ', value: '确户数'}
          ]
        } else if (state.energyType === '1') {
          arrHead = [
            { name: 'XZQHMC', value: '行政区划'},
            { name: 'GZCS_MGQ', value: '计划改造村数'},
            { name: 'GZCS_MGQ_SJ', value: '确村数'},
            { name: 'GZHS_MGQ', value: '计划改造户数'},
            { name: 'GZHS_MGQ_SJ', value: '确户数'}
          ]
        } else if (state.energyType === '2') {
          arrHead = [
            { name: 'XZQHMC', value: '行政区划'},
            { name: 'GZCS_MGD', value: '计划改造村数'},
            { name: 'GZCS_MGD_SJ', value: '确村数'},
            { name: 'GZHS_MGD', value: '计划改造户数'},
            { name: 'GZHS_MGD_SJ', value: '确户数'}
          ]
        }
        commit('changeGridHead', arrHead)
        commit('changeGridData', res)
        vm.$nextTick(() => {
          vm.$notice.loading.hide()
        })
      })
    }
  }
})
